﻿using Autofac.Core;
using WLYT.Application.Interfaces;
using WLYT.Application.Services;
using WLYT.Domain.Entries;

namespace WLYT.WMS.Component.Async
{

    /// <summary>
    /// 定时服务
    /// </summary>
    public class InStockUploadService(ILogger<InStockUploadService> logger, IConfiguration configuration, IServiceProvider services) : BackgroundService
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    if (configuration.GetSection("IsOpen").Get<int>() == 1)
                    {
                        using var scope = services.CreateScope();
                        var orderService = scope.ServiceProvider.GetRequiredService<IOrderService>();
                        await orderService.InStockUpload();
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "执行【InStockUploadService】定时任务失败");
                }
                await Task.Delay(5000);
            }
        }
    }

    /// <summary>
    /// 定时服务
    /// </summary>
    public class AgvTaskService(ILogger<AgvTaskService> logger, IConfiguration configuration, IServiceProvider services) : BackgroundService
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    if (configuration.GetSection("IsOpen").Get<int>() == 1)
                    {
                        using var scope = services.CreateScope();
                        var agvService = scope.ServiceProvider.GetRequiredService<IAgvService>();
                        await agvService.CreateAgvTask();
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "执行【CreateAgvTask】定时任务失败");
                }
                await Task.Delay(5000);
            }
        }
    }

    /// <summary>
    /// 定时服务
    /// </summary>
    public class ServiceA(ILogger<ServiceA> logger, IConfiguration configuration, IServiceProvider services) : BackgroundService
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    if (configuration.GetSection("IsOpen").Get<int>() == 1)
                    {
                        using var scope = services.CreateScope();
                        var orderService = scope.ServiceProvider.GetRequiredService<IOrderService>();
                        await orderService.OutStockUpload();
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "执行【OutStockUpload】定时任务失败");
                }
                await Task.Delay(5000);
            }
        }
    }

    public class ServiceB(ILogger<ServiceB> logger, IConfiguration configuration, IServiceProvider services) : BackgroundService
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    if (configuration.GetSection("IsOpen").Get<int>() == 1)
                    {
                        using var scope = services.CreateScope();
                        var orderService = scope.ServiceProvider.GetRequiredService<IOrderService>();
                        await orderService.CreateWarehouseTask();
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "执行【CreateWarehouseTask】定时任务失败");
                }
                await Task.Delay(5000);
            }
        }
    }

    public class ServiceC(ILogger<ServiceC> logger, IConfiguration configuration, IServiceProvider services) : BackgroundService
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    if (configuration.GetSection("IsOpen").Get<int>() == 1)
                    {
                        using var scope = services.CreateScope();
                        var orderService = scope.ServiceProvider.GetRequiredService<IOrderService>();
                        await orderService.SendTask();
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "执行【SendTask】定时任务失败");
                }
                await Task.Delay(5000);
            }
        }
    }
}
